package tableRelations.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.Query;

public class BaseDAO<T,ID extends Serializable> {
	private Class<T> clazz=GenericsUtils.getSuperClassGenricType(getClass());
	private String ClassName=clazz.getName();
	private Session session=HibernateUtils.getSession();
	
	public List<T> findAll(){
		String hql="from "+ClassName;
		Query<T> query = session.createQuery(hql);
		return query.list();
	}
	
	public T findById(ID id) {
		return session.get(clazz, id);
	}
	
	public void save(T model) {
		session.save(model);
	}
	
	public void delete(T model) {
		session.delete(model);
	}
	
	public void deleteById(ID id) {
		session.delete(findById(id));
	}
	
	public List<T> findByPropty(String proptyName,Object value){
		String hql= "from "+ClassName+" t where t."+proptyName+" = ?1";
		Query<T> query = session.createQuery(hql);
		query.setParameter(1, value);
		return query.getResultList();
	}
}
